﻿using System;
using System.Collections.Generic; 
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Ether
{
    public sealed class AxisIndicator : GameComponent
    {
        //Fields
        private float axisLength = 3f;
        private BasicEffect myEffect;
        private bool drawLettering = false;

        private VertexBuffer vertexBuffer;

        //Construct
        public AxisIndicator(Game game, bool drawLettering, float length)
            : base(game)
        {
            axisLength = length;
            this.drawLettering = drawLettering;
        }

        //Methods
        public void Setup()
        {
            myEffect = new BasicEffect(Game.GraphicsDevice);
            myEffect.LightingEnabled = false;
            myEffect.VertexColorEnabled = true;

            int vertexCount = (drawLettering) ? 22 : 6;
            VertexPositionColor[] vertices = new VertexPositionColor[vertexCount];

            //X axis
            vertices[0] = new VertexPositionColor(new Vector3(-axisLength, 0, 0), Color.Red);
            vertices[1] = new VertexPositionColor(new Vector3(axisLength, 0, 0), Color.Red);

            //Y axis
            vertices[2] = new VertexPositionColor(new Vector3(0, -axisLength, 0), Color.Yellow);
            vertices[3] = new VertexPositionColor(new Vector3(0, axisLength, 0), Color.Yellow);

            //Z axis
            vertices[4] = new VertexPositionColor(new Vector3(0, 0, -axisLength), Color.Blue);
            vertices[5] = new VertexPositionColor(new Vector3(0, 0, axisLength), Color.Blue);

            if (drawLettering)
            {
                #region drawing letters
                // "X" letter near X axis
                vertices[6] = new VertexPositionColor(
                new Vector3(axisLength - 0.1f, 0.05f, 0.0f), Color.Red);
                vertices[7] = new VertexPositionColor(
                new Vector3(axisLength - 0.05f, 0.2f, 0.0f), Color.Red);
                vertices[8] = new VertexPositionColor(
                new Vector3(axisLength - 0.05f, 0.05f, 0.0f), Color.Red);
                vertices[9] = new VertexPositionColor(
                new Vector3(axisLength - 0.1f, 0.2f, 0.0f), Color.Red);

                // "Y" letter near Y axis
                vertices[10] = new VertexPositionColor(
                new Vector3(0.075f, axisLength - 0.125f, 0.0f), Color.Yellow);
                vertices[11] = new VertexPositionColor(
                new Vector3(0.075f, axisLength - 0.2f, 0.0f), Color.Yellow);
                vertices[12] = new VertexPositionColor(
                new Vector3(0.075f, axisLength - 0.125f, 0.0f), Color.Yellow);
                vertices[13] = new VertexPositionColor(
                new Vector3(0.1f, axisLength - 0.05f, 0.0f), Color.Yellow);
                vertices[14] = new VertexPositionColor(
                new Vector3(0.075f, axisLength - 0.125f, 0.0f), Color.Yellow);
                vertices[15] = new VertexPositionColor(
                new Vector3(0.05f, axisLength - 0.05f, 0.0f), Color.Yellow);

                // "Z" letter near Z axis
                vertices[16] = new VertexPositionColor(
                new Vector3(0.0f, 0.05f, axisLength - 0.1f), Color.Blue);
                vertices[17] = new VertexPositionColor(
                new Vector3(0.0f, 0.05f, axisLength - 0.05f), Color.Blue);
                vertices[18] = new VertexPositionColor(
                new Vector3(0.0f, 0.05f, axisLength - 0.1f), Color.Blue);
                vertices[19] = new VertexPositionColor(
                new Vector3(0.0f, 0.2f, axisLength - 0.05f), Color.Blue);
                vertices[20] = new VertexPositionColor(
                new Vector3(0.0f, 0.2f, axisLength - 0.1f), Color.Blue);
                vertices[21] = new VertexPositionColor(
                new Vector3(0.0f, 0.2f, axisLength - 0.05f), Color.Blue);

                #endregion
            }

            vertexBuffer = new VertexBuffer(Game.GraphicsDevice, typeof(VertexPositionColor), vertexCount, BufferUsage.WriteOnly);
            vertexBuffer.SetData<VertexPositionColor>(vertices);

        }

        public void Draw(Camera camera, GameTime time)
        {
            if (vertexBuffer == null)
                Setup();

            myEffect.Projection = camera.Projection;
            myEffect.View = camera.View;
            myEffect.World = Matrix.Identity;


            foreach (EffectPass effectPass in myEffect.CurrentTechnique.Passes)
            {
                effectPass.Apply();
                Game.GraphicsDevice.SetVertexBuffer(vertexBuffer);
                Game.GraphicsDevice.DrawPrimitives(PrimitiveType.LineList, 0, (drawLettering) ? 11 : 3);
            }
        }
    }
}
